import logging
import sqlite3

from utils.lzsd_utils import LZSDUtils

logger = logging.getLogger(__name__)

_db_file_path = LZSDUtils.get_app_dir_file_path("db.sqlite3")
logger.info("database file path: {}".format(_db_file_path))

_db_conn = sqlite3.connect(_db_file_path)

def db_new_connection():
    conn = None
    try:
        conn = sqlite3.connect(_db_file_path)  # 使用 sqlite3 连接数据库
        if conn:
            conn.row_factory = sqlite3.Row  # 让查询结果返回为字典格式
    except Exception as e:
        logger.info("e:{}".format(e))
        conn = None
    return conn


def db_close(conn):
    if conn:
        conn.close()


db_share = db_new_connection()